import scapy.all as scapy
import os
import re
import pymysql
import time


def run():
    print("====================解析程序====================\n")
    pattern = re.compile(
        r'GET /(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')

    path = "/root/pcap"

    sql = "insert into url(url,time) values "
    first = False

    for file in os.listdir(path):

        file_path = path + "/" + file
        print('=====> 解析文件 [', file_path, ' ]中 <=====')
        packets = scapy.rdpcap(file_path)

        for p in packets:
            for f in p.payload.payload.payload.fields_desc:
                fvalue = p.payload.payload.getfieldval(f.name)
                reprval = f.i2repr(p.payload.payload, fvalue)
                if 'HTTP' in reprval:
                    lst = str(reprval).split(r'\r\n')
                    for l in lst:
                        url = re.findall(pattern, l)
                        url = "".join(url)
                        if url != "":
                            if not first:
                                first = True
                            else:
                                sql += ','
                            sql += "('" + url + "','" + \
                                   time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "')"
                        else:
                            continue
                else:
                    break
        os.remove(file_path)
    conn = pymysql.connect(host="127.0.0.1", user="root", passwd="password", db='ep2', port=3306, autocommit=True)
    cursor = conn.cursor()
    cursor.execute("""use ep2""")
    if first:
        cursor.execute(sql)
    cursor.close()
    conn.close()
    print("=====> 文件解析完毕 <=====")
    print("\n===============================================")


if __name__ == '__main__':
    run()
